Skip tautological evaluators in MessageEvaluator.append#454
Merged
pkwarren merged 2 commits intobufbuild:mainfrom Apr 23, 2026
Merged
Skip tautological evaluators in MessageEvaluator.append#454pkwarren merged 2 commits intobufbuild:mainfrom
pkwarren merged 2 commits intobufbuild:mainfrom
Conversation
MessageEvaluator.append previously stored every evaluator unconditionally, so FieldEvaluators for fields with no rules still ran on every validate() call: hasField check, ObjectValue allocation, ValueEvaluator.evaluate with a zero-length evaluator list, return empty. None of that can ever produce a violation, so iterating them is pure overhead. ValueEvaluator.append already filters on tautology(); this change mirrors that in MessageEvaluator to match protovalidate-go's AppendNested (internal/evaluator/message.go:67-77), which skips appends whose Tautology() is true. The effect scales with the fraction of no-rule fields in a message. On a 2-field message (1 ruled, 1 unruled), per-call validate() dropped from ~977 ns to ~908 ns (~7%). Messages with more unconstrained fields save proportionally more.
pkwarren
added a commit
that referenced
this pull request
Apr 23, 2026
In order to verify performance fixes (like #451 and #454), it would be helpful to be able to write a JMH benchmark and quickly measure the impact. This adds a benchmark module to the Gradle build. CI only verifies the build of the benchmarks. Added a README showing how to add benchmarks and measure improvements locally. Instead of outputting JSON or other format, created a jq script to roughly print out output similar to benchstat for Go.
pkwarren
added a commit
that referenced
this pull request
Apr 23, 2026
In order to verify performance fixes (like #451 and #454), it would be helpful to be able to write a JMH benchmark and quickly measure the impact. This adds a benchmark module to the Gradle build. CI only verifies the build of the benchmarks. Added a README showing how to add benchmarks and measure improvements locally. Instead of outputting JSON or other format, created a jq script to roughly print out output similar to benchstat for Go.
pkwarren
approved these changes
Apr 23, 2026
Member
pkwarren
left a comment
There was a problem hiding this comment.
Changes look good to me. Thanks for the fix!
> Task :benchmarks:jmhCompare
benchmark metric before after delta
validateManyUnruled time 625.57 ns/op 407.94 ns/op -34.8%
validateManyUnruled alloc 3140 B/op 2088 B/op -33.5%
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MessageEvaluator.appendstored every evaluator unconditionally, soFieldEvaluators for fields with no rules ran on everyvalidate()call:hasFieldcheck,ObjectValueallocation,ValueEvaluator.evaluatewith a zero-length evaluator list, return empty. None of that can ever produce a violation, so iterating those entries is pure overhead.ValueEvaluator.appendalready filters ontautology(); this mirrors the check inMessageEvaluator.appendto match protovalidate-go'sAppendNested(internal/evaluator/message.go:67-77), which skips appends whoseTautology()is true.Impact
Measured on a message with 2 fields (1 ruled, 1 unruled) over 1M iterations with a warm JIT:
The effect scales with the fraction of no-rule fields. Messages with many unconstrained fields (common in real-world proto definitions where validation targets a subset) save proportionally more.
Test plan
./gradlew testpasses (unit + conformance)